package br.com.sovi.gwt.client.rastreamento;


import br.com.sovi.gwt.client.ImagesImpl;
import br.com.sovi.gwt.client.entity.rastreamento.Rastreavel;

import com.gwtext.client.widgets.Component;
import com.gwtext.client.widgets.form.Label;
import com.gwtext.client.widgets.portal.Portal;
import com.gwtext.client.widgets.portal.PortalColumn;
import com.gwtext.client.widgets.portal.Portlet;

public class RastreamentoPortal extends Portal implements
		RastreavelCheckChangeListener {

	private int numeroColunas = 3;
	
	private PortalColumn[] columns = null;
	
	private Label messageLabel;

	public RastreamentoPortal() {
		init();
	}

	private void init() {
		this.setAutoScroll(true);
		this.setBorder(false);

		buildEmptyPortalMessage();
		this.add(messageLabel);
		
		columns = new PortalColumn[numeroColunas];

		for (int i = 0; i < columns.length; i++) {
			// create portal columns
			PortalColumn column = new PortalColumn();
			column.setPaddings(10, 10, 0, 10);

			columns[i] = column;
			this.add(column);
		}
	}
	
	public void buildEmptyPortalMessage() {
		StringBuilder sb = new StringBuilder();
		sb.append("<table><tr>");
		sb.append("<td>").append(ImagesImpl.get().left().getHTML()).append("</td>");
		sb.append("<td><div style='font-size: 12px; font-weight: bold; font-family: Arial'>Selecione um ou mais rastreamentos ao lado para ver no mapa.</div></td>");
		
		messageLabel = new Label();
		messageLabel.setHtml(sb.toString());
	}
	
	public void hideEmptyPortalMessage() {
		this.remove(messageLabel);
		messageLabel.setVisible(false);
	}
	
	private Portlet createPortlet(final Rastreavel rastreavel) {
		return new RastreavelMapPortlet(rastreavel);
	}

	public void addPortletBalanced(Portlet portlet) {
		int c = 0;

		int[] sizes = new int[columns.length];

		for (int i = 0; i < columns.length; i++) {
			sizes[i] = columns[i].getItems().length;
		}

		c = 0;

		for (int i = 0; i < sizes.length; i++) {
			if (sizes[c] > sizes[i]) {
				c = i;
			}
		}

		this.addPortlet(c, portlet);
	}

	public void addPortlet(int columnIndex, Portlet portlet) {
		PortalColumn column = columns[columnIndex];

		if (column == null) {
			column = columns[0];
		}

		column.add(portlet);
		column.doLayout();
		
		hideEmptyPortalMessage();
	}

	@Override
	public void onCheckChange(Rastreavel rastreavel, boolean checked) {
		if (checked) {
			if (findPortletByRastreavel(rastreavel) == null) {
				addPortletBalanced(createPortlet(rastreavel));
			}
		} else {
			removePortletByRastreavel(rastreavel);
		}
	}

	private Portlet findPortletByRastreavel(Rastreavel rastreavel) {
		for (PortalColumn column : columns) {
			Component[] items = column.getItems();

			for (Component component : items) {
				if (component instanceof Portlet) {
					RastreavelMapPortlet portlet = (RastreavelMapPortlet) component;

					if (rastreavel.equals(portlet.getRastreavel())) {
						return portlet;
					}
				}
			}
		}

		return null;
	}

	private PortalColumn findPortalColumnFromPortlet(Portlet portlet) {
		for (PortalColumn column : columns) {
			Component[] items = column.getItems();

			for (Component component : items) {
				if (component instanceof Portlet) {
					Portlet loop = (Portlet) component;

					if (loop.equals(portlet)) {
						return column;
					}
				}
			}
		}
		return null;
	}

	private void removePortletByRastreavel(Rastreavel rastreavel) {
		RastreavelMapPortlet portlet = (RastreavelMapPortlet) findPortletByRastreavel(rastreavel);

		if (portlet != null) {
			portlet.stopRastreamento();
			
			PortalColumn column = findPortalColumnFromPortlet(portlet);
			if (column != null) {
				column.remove(portlet);
				column.doLayout();
			}
		}
	}

}
